package com.sxkf.template.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxkf.template.entity.SysConfig;
import com.sxkf.template.service.SysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.util.RedisUtils;
import org.springframework.web.bind.annotation.*;
import com.sxkf.core.api.vo.R;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张广成
 * @since 2024-11-05 12:03:39
 */
@Api(tags = "系统参数模块")
@RestController
@RequestMapping("/sysConfig")
public class SysConfigController {

    @Resource
    private SysConfigService sysConfigService;


    @ApiOperation("全查系统配置")
    @GetMapping("/selectAll")
    public R<List<SysConfig>> selectAll(){
        List<SysConfig> list = sysConfigService.list();
        return R.success(list);
    }

    @ApiOperation("分页查系统配置")
    @GetMapping("/page")
    public R<IPage<SysConfig>> page(@RequestParam Integer pageNo,
                                       @RequestParam(defaultValue = "10") Integer pageSize){
        Page<SysConfig> page = sysConfigService.page(new Page<>(pageNo, pageSize));
        return R.success(page);
    }

    @ApiOperation("按照key查一个系统配置")
    @GetMapping("/one")
    public R<SysConfig> one(@RequestParam String configKey){
        SysConfig one = sysConfigService.getOne(new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getConfigKey, configKey));
        return R.success(one);
    }

    @ApiOperation("按照key查一个系统配置的value")
    @GetMapping("/oneValue")
    public R<String> oneValue(@RequestParam String configKey){
        SysConfig one = sysConfigService.getOne(new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getConfigKey, configKey));
        return R.success(one.getConfigValue());
    }

    @ApiOperation("添加系统配置")
    @PostMapping("/insert")
    public R<Boolean> insert(@RequestBody SysConfig systemConfig){
        boolean save = sysConfigService.save(systemConfig);
//        sysConfigService.updateSystemConfig(systemConfig);
        return R.success(save);
    }

    @ApiOperation("修改系统配置")
    @PostMapping("/update")
    public R<Boolean> update(@RequestBody SysConfig systemConfig){
        boolean update = sysConfigService.edit(systemConfig);
//        sysConfigService.updateSystemConfig(systemConfig);
        return R.success(update);
    }

    @ApiOperation("删除系统配置")
    @GetMapping("/delete")
    public R delete(@RequestParam String configId){
        sysConfigService.removeById(configId);
        return R.success();
    }

}
